<?php

	require_once("fta.201003310000/fta.php");
	fta_registerModules(
		array(
		"cms.201003310000/config/cmsConfig",//we will deprecate the old cms config soon..
		"FTAWebPage/ftaDefaultWidgets"
	));

class MAIClasses extends FTAObject {

		var $renderClassesListFunction;
		var $classType;
		var $classGroups;

		function MAIClasses(){
			parent::__construct();

			global $db;
			$this->cmsDocument = new CmsDocument($db);
		}

		function getClass($class){

			//add additional cache checks here before we do all this shit..
			if (is_null($class))
				return false;

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docClasses');

			//Limit the classes displayed to those that have spaces
			//$this->cmsDocument->addConstraint("authordepartment", 0, ">");

			if (is_numeric( ($class * 1) ) &&  0 < ($class * 1)){
				$this->cmsDocument->addConstraint('documentcode',$class);
			} else {
				$this->cmsDocument->addConstraint('<<title>>',$class);
			}

			$this->cmsDocument->setPageSize(1);

			$this->cmsDocument->resetIterator();

			if($this->cmsDocument->totalDocuments() == 1) {
				return $this->cmsDocument->nextDocumentFromCache();
			} else {
				return false;
			}
		}

		function getClasses($pageSize = null, $pageNumber = null, $type = null, $sort = null, $direction = null){

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docClasses');

			//Limit the classes displayed to those that have spaces
			//$this->cmsDocument->addConstraint("authordepartment", 0, ">");

			if(!is_null($type)){
				$this->cmsDocument->addConstraint("restricted", $type);
			}
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("address");
			}

			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);

			$this->cmsDocument->resetIterator();

			$allDocuments = $this->cmsDocument->getAllDocuments();

			if (!$allDocuments)
				$allDocuments = array();

			return $allDocuments;

		}

		function getClassTypeByUrl($url = null){

			if(is_null($url)){
				return false;
			}

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docClassType');
			if(!is_null($url)){
				$this->cmsDocument->addConstraint("<<md.urlfragment>>", $url);
			}
			$this->cmsDocument->addSort("address");

			$this->cmsDocument->setPageSize(1);
			$this->cmsDocument->setPageNumber(0);

			$this->cmsDocument->resetIterator();

			$allDocuments = $this->cmsDocument->getAllDocuments();

			//echo "class type = <pre>";print_r($allDocuments);echo "<pre>";

			if($this->cmsDocument->totalDocuments() == 1) {
				$this->classType = $allDocuments[0];
				return $allDocuments[0];
			} else {
				$this->classType = array();
				return false;
			}

		}

		function getClassGroupsForType($typeFragment = null){

			$classType = $this->getClassTypeByUrl($typeFragment);
			if(!$classType){
				return false;
			}

			//Get array of documentcodes related to classType
			global $db;
			$query = "select relatedcode from manytomanydata where parentcode = '" . $classType->documentcode . "' and elementid = '20195' ";
					
			$result = $db->executeQuery($query);

			$classGroups = array();

			while($row = $db->nextRow()){
				$classGroups[] = $row[0];
			}

			//Pull out sorted list of objects of classGroup related
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docClassGrp');
			if(count($classGroups) > 0){
				$this->cmsDocument->addConstraint('documentcode', "('" . implode("', '", $classGroups) . "' )", " IN ");
			} else {
				return false;
			}
			$this->cmsDocument->addSort("<<md.rank>>");

			$this->cmsDocument->setPageSize(null);
			$this->cmsDocument->setPageNumber(null);

			$this->cmsDocument->resetIterator();

			$allDocuments = $this->cmsDocument->getAllDocuments();

			if (!$allDocuments)
				$allDocuments = array();

			$this->classGroups = $allDocuments;

			return $allDocuments;
		}

		function getClassesByGroup($group = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docClasses');

			//Limit the classes displayed to those that have spaces
			//$this->cmsDocument->addConstraint("authordepartment", 0, ">");

			if(!is_null($group)){
				$this->cmsDocument->addConstraint("url", $group);
			}
			/*if(!is_null($type)){
				$this->cmsDocument->addConstraint("restricted", $type);
			}*/
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				// $this->cmsDocument->addSort("startdate");
				$this->cmsDocument->addSort('<<md.rank>>', 'ASC');
			}

			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);

			$this->cmsDocument->resetIterator();

			$allDocuments = $this->cmsDocument->getAllDocuments();

			if (!$allDocuments)
				$allDocuments = array();

			return $allDocuments;

		}

		function renderClassTabs($typeFragment = null, $sort = null, $direction = null){
			// 1.  Pull out class
			$classType = $this->getClassTypeByUrl($typeFragment);
			if(!$classType){
				return false;
			}

			// 2.  Get groups for type
			$classGroups = $this->getClassGroupsForType($typeFragment);
			if(count($classGroups) < 1){
				return false;
			}

			// 3.  Pull out classes and groups into arrays for rendering
			$classGroupList = array();
			$classGroupDocuments = array();
			foreach ($classGroups as $group){
				//get classes for group
				$groupClasses = $this->getClassesByGroup($group->documentcode, null, null, $sort, $direction);
				if(count($groupClasses) > 0){
					//add the group and classes to array
					$classGroupList[$group->documentcode] = $groupClasses;
					//echo "group classes = <pre>";print_r($groupClasses);echo "</pr>";

					$classGroupDocuments[$group->documentcode] = $group;

				}
			}

			// 3.  Render the top section for groups
			?>
			<ul class="anchor-nav">

			<?php
				$row = 1;
				foreach ($classGroups as $group){

					//if(isset($classGroupList[$group->documentcode])) {

						if($row == count($classGroups)){
							?>
							<li class="last-item"><a href="#grp-<?php echo $group->documentcode; ?>"><?php echo $group->title; ?></a></li>
							<?php
						} else {
							?>
                            <li><a href="#grp-<?php echo $group->documentcode; ?>"><?php echo $group->title; ?></a></li>
							<?php
						}
						$row++;

					//}

				}
			?>
			</ul>
			<div id="s4">
			<?php

			// 4.  Render the items for each Group
			//foreach ($classGroupList as $key=>$group){
			foreach ($classGroups as $group){
				//echo "Class Groups List= <pre>"; print_r($classGroupList); echo "</pre>";
				//$groupDocument = $classGroupDocuments[$key];
				?>
				<div class="content-block" id="grp-<?php echo $group->documentcode; ?>">
	            <a name="<?php echo $group->title; ?>"></a><h2 id="<?php echo $group->title; ?>"><?php echo $group->title; ?></h2>
	            <?php
	            if(isset($classGroupList[$group->documentcode])){
	            ?>
		            <ul class="class-list">
					<?php

					foreach($classGroupList[$group->documentcode] as $item){
						$this->renderClassItem($item);
					}
					?>
					</ul>
            	<?php
				} else {
            	?>
            		No Classes for this period.
            	<?php
				}
				?>
            	</div><!-- content-block ends -->

				<?php
			}
			?>
			</div>
			<?php
		}

		function renderClassesList($group = null, $pageSize = null, $pageNumber = null, $type = null, $sort = null, $direction = null){

			$classes = $this->getClassesByGroup($group, $pageSize, $pageNumber, $sort, $direction);

			if (0 == count($classes))	return;

			foreach ($classes as $item){
				echo $this->renderClassItem($item);
			}
		}

		function renderClassItem($item){

		$tutor = $this->cmsDocument->openDocument($item->extra1);
		?>
		<li>
		<?php
		if(strlen($item->extra11) > 0 ){
		?>
			<img src="/html/blob.php?documentCode=<?php echo $item->documentcode; ?>&elementId=20200&filetypecode=2" class="img-l" />
		<?php
		} else {
		?>
			<img src="/images/placeHolder-class.gif" class="img-l" />
		<?php
		}
		?>
    	<div>
        <strong><?php echo $item->title; ?>
        with <a href='/About-Us/Tutor/<?php echo urlFragmentClean($tutor->title); ?>/'><?php echo $tutor->title; ?></a></strong>
        <br>

        <?php echo $item->shortdescription; ?>

		<br>
        <span class="class-slots">
        	<strong><?php echo $item->authordepartment; ?></strong> spaces left
        </span>
        <br>
        <?php if($item->extra13 == '1' && ($item->authordepartment > 0)) { ?>
        	<?php //<ul class="nav-list"><li><a href="/Classes/Enrolment-Form/">Enrol now</a></li></ul> ?>
        	<form name="submit-enrolement-<?php echo $item->documentcode; ?>" id="submit-enrolment-<?php echo $item->documentcode; ?>" method="POST" action="/Classes/Online-Enrolment/"><input type="hidden" name="productId" value="<?php echo $item->documentcode; ?>" /><input class="class-enrol-submit" type="submit" value="Enrol" /></form>
        <?php } else { ?>
        	<a href="/Contact-Us/" class="class-full">Class full - please contact us</a>
        <?php } ?>
        </div>
        </li>
		<?php
	}

		function renderPageTitle($urlFragment){
			$classType = $this->getClassTypeByUrl($urlFragment);
			echo $classType->pagetitle;
		}

		function renderPageHeading($urlFragment){
			$classType = $this->getClassTypeByUrl($urlFragment);
			echo $classType->heading;
		}

		function renderMetadataDescription($urlFragment){
			$classType = $this->getClassTypeByUrl($urlFragment);
			echo $classType->description;
		}

		function renderMetadataKeywords($urlFragment){
			$classType = $this->getClassTypeByUrl($urlFragment);
			echo $classType->keywords;
		}

		function renderIntro($urlFragment){
			$classType = $this->getClassTypeByUrl($urlFragment);
			echo $classType->shortdescription;
		}
}

	function mai_renderClassList($typeFragment = null, $sort = null, $direction = null){
		ob_start();
		$classesObject = mai_getSharedClassesObject();
		$classesObject->renderClassTabs($typeFragment, $sort, $direction);
		return ob_get_clean();
	}

	function mai_renderMetaDataDescription($typeFragment = null){
		ob_start();
		$classesObject = mai_getSharedClassesObject();
		$classesObject->renderMetadataDescription($typeFragment);
		return ob_get_clean();
	}

	function mai_renderMetaDataKeywords($typeFragment = null){
		ob_start();
		$classesObject = mai_getSharedClassesObject();
		$classesObject->renderMetadataKeywords($typeFragment);
		return ob_get_clean();
	}

	function mai_renderPageTitle($typeFragment = null){
		ob_start();
		$classesObject = mai_getSharedClassesObject();
		$classesObject->renderPageTitle($typeFragment);
		return ob_get_clean();
	}

	function mai_renderPageHeading($typeFragment = null){
		ob_start();
		$classesObject = mai_getSharedClassesObject();
		$classesObject->renderPageHeading($typeFragment);
		return ob_get_clean();
	}

	function mai_renderIntro($typeFragment = null){
		ob_start();
		$classesObject = mai_getSharedClassesObject();
		$classesObject->renderIntro($typeFragment);
		return ob_get_clean();
	}

	function mai_getSharedClassesObject(){

		global $fta_;

		global $MAIClassesClass;

		if(!is_null($MAIClassesClass) && "MAIClasses" != $MAIClassesClass){
		} else {
			$MAIClassesClass = "MAIClasses";
		}

		if (!array_key_exists('maiClasses',$fta_)){
			$fta_->maiClasses = new  $MAIClassesClass();
		}
		return $fta_->maiClasses;
	}


?>
